<?php require_once('../connections/con_gl.php'); ?>
<?php
$act = $_POST['act'];
$url = "index.php?component=customercorporate";
$nop = strtoupper($_POST['tlp']);
$ope = $_POST['open'];

/* Validasi */
if (trim($_POST['nama']) == '') {
	$error[] = '- Nama harus diisi !!!';
}
if (trim($_POST['alamat']) == '') {
	$error[] = '- Alamat harus diisi !!!';
}
if (trim($_POST['tlp']) == '') {
	$error[] = '- No Telpon harus diisi !!!';
}
if (trim($_POST['email']) <> '' AND !eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST[email])){ 
	$error[] = '- Format Email abcde@xxxx.com  !!!';
}
if($act=='add' AND (trim($_POST['tlp']) <> '')) {
	// cek no polisi
	mysql_select_db($database_con_gl, $con_gl);
	$query_cekno = "select tlp from gl_kontrak where tlp='$nop'";
	$cekno       = mysql_query($query_cekno, $con_gl);
	$row_cekno   = mysql_fetch_assoc($cekno);
	$total_cekno = mysql_num_rows($cekno);
	if($total_cekno > 0) { 
		$error[] = '- Nama Customer '.$nop.' sudah terdaftar';
	}
}

/*End validasi */
if (isset($error)) {
	echo "<b>Error</b>: <br />".implode("<br />", $error);
}else{
	switch($act){
		case 'add' :
			//Upload file pendukung
			if($_POST['copyakta']=='ok'){
				if(is_uploaded_file($_FILES['filecopyakta']['tmp_name'])){
					$akta = $_FILES['filecopyakta']['tmp_name'];
					$aktares = "documents/".$_FILES['filecopyakta']['name'];
					move_uploaded_file($akta, "../".$aktares);
				}
			}else $aktares = "";
			if($_POST['copysiup']=='ok'){
				if(is_uploaded_file($_FILES['filecopysiup']['tmp_name'])){
					$siup = $_FILES['filecopysiup']['tmp_name'];
					$siupres = "documents/".$_FILES['filecopysiup']['name'];
					move_uploaded_file($siup, "../".$siupres);
				}
			}else $siupres = "";
			if($_POST['copytdp']=='ok'){
				if(is_uploaded_file($_FILES['filecopytdp']['tmp_name'])){
					$tdp = $_FILES['filecopytdp']['tmp_name'];
					$tdpres = "documents/".$_FILES['filecopytdp']['name'];
					move_uploaded_file($tdp, "../".$tdpres);
				}
			}else $tdpres = "";
			if($_POST['copydomisili']=='ok'){
				if(is_uploaded_file($_FILES['filecopydomisili']['tmp_name'])){
					$domisili = $_FILES['filecopydomisili']['tmp_name'];
					$domisilires = "documents/".$_FILES['filecopydomisili']['name'];
					move_uploaded_file($domisili, "../".$domisilires);
				}
			}else $domisilires = "";
			if($_POST['copynpwp']=='ok'){
				if(is_uploaded_file($_FILES['filecopynpwp']['tmp_name'])){
					$npwp = $_FILES['filecopynpwp']['tmp_name'];
					$npwpres = "documents/".$_FILES['filecopynpwp']['name'];
					move_uploaded_file($npwp, "../".$npwpres);
				}
			}else $npwpres = "";
			if($_POST['copyktp1']=='ok'){
				if(is_uploaded_file($_FILES['filecopyktp1']['tmp_name'])){
					$ktp1 = $_FILES['filecopyktp1']['tmp_name'];
					$ktp1res = "documents/".$_FILES['filecopyktp1']['name'];
					move_uploaded_file($ktp1, "../".$ktp1res);
				}
			}else $ktp1res = "";
			if($_POST['copyktp2']=='ok'){
				if(is_uploaded_file($_FILES['filecopyktp2']['tmp_name'])){
					$ktp2 = $_FILES['filecopyktp2']['tmp_name'];
					$ktp2res = "documents/".$_FILES['filecopyktp2']['name'];
					move_uploaded_file($ktp2, "../".$ktp2res);
				}
			}else $ktp2res = "";
			if($_POST['copysim']=='ok'){
				if(is_uploaded_file($_FILES['filecopysim']['tmp_name'])){
					$sim = $_FILES['filecopysim']['tmp_name'];
					$simres = "documents/".$_FILES['filecopysim']['name'];
					move_uploaded_file($sim, "../".$simres);
				}
			}else $simres = "";
			if($_POST['copypo']=='ok'){
				if(is_uploaded_file($_FILES['filecopypo']['tmp_name'])){
					$po = $_FILES['filecopypo']['tmp_name'];
					$pores = "documents/".$_FILES['filecopypo']['name'];
					move_uploaded_file($po, "../".$pores);
				}
			}else $pores = "";
			if($_POST['copytabungan']=='ok'){
				if(is_uploaded_file($_FILES['filecopytabungan']['tmp_name'])){
					$tabungan = $_FILES['filecopytabungan']['tmp_name'];
					$tabunganres = "documents/".$_FILES['filecopytabungan']['name'];
					move_uploaded_file($tabungan, "../".$tabunganres);
				}
			}else $tabunganres = "";
			if($_POST['copykk']=='ok'){
				if(is_uploaded_file($_FILES['filecopykk']['tmp_name'])){
					$kk = $_FILES['filecopykk']['tmp_name'];
					$kkres = "documents/".$_FILES['filecopykk']['name'];
					move_uploaded_file($kk, "../".$kkres);
				}
			}else $kkres = "";
			if($_POST['copypbb']=='ok'){
				if(is_uploaded_file($_FILES['filecopypbb']['tmp_name'])){
					$pbb = $_FILES['filecopypbb']['tmp_name'];
					$pbbres = "documents/".$_FILES['filecopypbb']['name'];
					move_uploaded_file($pbb, "../".$pbbres);
				}
			}else $pbbres = "";
			if($_POST['copysurat']=='ok'){
				if(is_uploaded_file($_FILES['filecopysurat']['tmp_name'])){
					$surat = $_FILES['filecopysurat']['tmp_name'];
					$suratres = "documents/".$_FILES['filecopysurat']['name'];
					move_uploaded_file($surat, "../".$suratres);
				}
			}else $suratres = ""; 
			$query = "INSERT INTO gl_kontak (id, type, tcustomer, nama, tgldaftar, area, cabang, gudang, npwp, alamat, tlp, fax, email, status, keterangan, badanusaha, spesifikasiusaha, jmlkaryawan, omset, nama1, jabatan1, alamat1, tlp1, nama2, jabatan2, alamat2, tlp2, nama3, jabatan3, alamat3, tlp3, copyakta, copysiup, copytdp, copydomisili, copynpwp, copyktp1, copyktp2, copysim, copypo, copytabungan, copykk, copypbb,copysurat) VALUES (NULL, 'Customer', 'Corporate', '$_POST[nama]', '$_POST[tgldaftar]', '$_POST[area]', '$_POST[cabang]','$_POST[cabang]','$_POST[npwp]','$_POST[alamat]', '$_POST[tlp]', '$_POST[fax]','$_POST[email]', '$_POST[statuscustomer]', '$_POST[keterangan]', '$_POST[badanusaha]', '$_POST[spesifikasiusaha]', '$_POST[jmlkaryawan]', '$_POST[omset]', '$_POST[nama1]', '$_POST[jabatan1]', '$_POST[alamat1]', '$_POST[tlp1]', '$_POST[nama2]', '$_POST[jabatan2]', '$_POST[alamat2]', '$_POST[tlp2]','$_POST[nama3]', '$_POST[jabatan3]', '$_POST[alamat3]', '$_POST[tlp3]', '$aktares', '$siupres', '$tdpres', '$domisilires', '$npwpres', '$ktp1res', '$ktp2res', '$simres', '$pores', '$tabunganres', '$kkres', '$pbbres', '$suratres')"; //echo $query;
			break;
		case 'edit' :
			//Upload file pendukung
			if($_POST['copyakta']=='ok'){
				if(is_uploaded_file($_FILES['filecopyakta']['tmp_name'])){
					$akta = $_FILES['filecopyakta']['tmp_name'];
					$aktares = "documents/".$_FILES['filecopyakta']['name'];
					move_uploaded_file($akta, "../".$aktares);
				}
			}else $aktares = "";
			if($_POST['copysiup']=='ok'){
				if(is_uploaded_file($_FILES['filecopysiup']['tmp_name'])){
					$siup = $_FILES['filecopysiup']['tmp_name'];
					$siupres = "documents/".$_FILES['filecopysiup']['name'];
					move_uploaded_file($siup, "../".$siupres);
				}
			}else $siupres = "";
			if($_POST['copytdp']=='ok'){
				if(is_uploaded_file($_FILES['filecopytdp']['tmp_name'])){
					$tdp = $_FILES['filecopytdp']['tmp_name'];
					$tdpres = "documents/".$_FILES['filecopytdp']['name'];
					move_uploaded_file($tdp, "../".$tdpres);
				}
			}else $tdpres = "";
			if($_POST['copydomisili']=='ok'){
				if(is_uploaded_file($_FILES['filecopydomisili']['tmp_name'])){
					$domisili = $_FILES['filecopydomisili']['tmp_name'];
					$domisilires = "documents/".$_FILES['filecopydomisili']['name'];
					move_uploaded_file($domisili, "../".$domisilires);
				}
			}else $domisilires = "";
			if($_POST['copynpwp']=='ok'){
				if(is_uploaded_file($_FILES['filecopynpwp']['tmp_name'])){
					$npwp = $_FILES['filecopynpwp']['tmp_name'];
					$npwpres = "documents/".$_FILES['filecopynpwp']['name'];
					move_uploaded_file($npwp, "../".$npwpres);
				}
			}else $npwpres = "";
			if($_POST['copyktp1']=='ok'){
				if(is_uploaded_file($_FILES['filecopyktp1']['tmp_name'])){
					$ktp1 = $_FILES['filecopyktp1']['tmp_name'];
					$ktp1res = "documents/".$_FILES['filecopyktp1']['name'];
					move_uploaded_file($ktp1, "../".$ktp1res);
				}
			}else $ktp1res = "";
			if($_POST['copyktp2']=='ok'){
				if(is_uploaded_file($_FILES['filecopyktp2']['tmp_name'])){
					$ktp2 = $_FILES['filecopyktp2']['tmp_name'];
					$ktp2res = "documents/".$_FILES['filecopyktp2']['name'];
					move_uploaded_file($ktp2, "../".$ktp2res);
				}
			}else $ktp2res = "";
			if($_POST['copysim']=='ok'){
				if(is_uploaded_file($_FILES['filecopysim']['tmp_name'])){
					$sim = $_FILES['filecopysim']['tmp_name'];
					$simres = "documents/".$_FILES['filecopysim']['name'];
					move_uploaded_file($sim, "../".$simres);
				}
			}else $simres = "";
			if($_POST['copypo']=='ok'){
				if(is_uploaded_file($_FILES['filecopypo']['tmp_name'])){
					$po = $_FILES['filecopypo']['tmp_name'];
					$pores = "documents/".$_FILES['filecopypo']['name'];
					move_uploaded_file($po, "../".$pores);
				}
			}else $pores = "";
			if($_POST['copytabungan']=='ok'){
				if(is_uploaded_file($_FILES['filecopytabungan']['tmp_name'])){
					$tabungan = $_FILES['filecopytabungan']['tmp_name'];
					$tabunganres = "documents/".$_FILES['filecopytabungan']['name'];
					move_uploaded_file($tabungan, "../".$tabunganres);
				}
			}else $tabunganres = "";
			if($_POST['copykk']=='ok'){
				if(is_uploaded_file($_FILES['filecopykk']['tmp_name'])){
					$kk = $_FILES['filecopykk']['tmp_name'];
					$kkres = "documents/".$_FILES['filecopykk']['name'];
					move_uploaded_file($kk, "../".$kkres);
				}
			}else $kkres = "";
			if($_POST['copypbb']=='ok'){
				if(is_uploaded_file($_FILES['filecopypbb']['tmp_name'])){
					$pbb = $_FILES['filecopypbb']['tmp_name'];
					$pbbres = "documents/".$_FILES['filecopypbb']['name'];
					move_uploaded_file($pbb, "../".$pbbres);
				}
			}else $pbbres = "";
			if($_POST['copysurat']=='ok'){
				if(is_uploaded_file($_FILES['filecopysurat']['tmp_name'])){
					$surat = $_FILES['filecopysurat']['tmp_name'];
					$suratres = "documents/".$_FILES['filecopysurat']['name'];
					move_uploaded_file($surat, "../".$suratres);
				}
			}else $suratres = ""; 
			$query ="UPDATE gl_kontak SET 
						type='Customer', 
						tcustomer='Corporate', 
						nama='$_POST[nama]', 
						tgldaftar='$_POST[tgldaftar]', 
						area='$_POST[area]', 
						cabang='$_POST[cabang]', 
						gudang='$_POST[cabang]', 
						npwp='$_POST[npwp]', 
						alamat='$_POST[alamat]', 
						tlp='$_POST[tlp]', 
						fax='$_POST[fax]', 
						email='$_POST[email]', 
						status='$_POST[statuscustomer]', 
						keterangan='$_POST[keterangan]',  
						badanusaha='$_POST[badanusaha]', 
						spesifikasiusaha='$_POST[spesifikasiusaha]', 
						jmlkaryawan='$_POST[jmlkaryawan]', 
						omset='$_POST[omset]', 
						nama1='$_POST[nama1]', 
						jabatan1='$_POST[jabatan1]', 
						alamat1='$_POST[alamat1]',
						tlp1='$_POST[tlp1]', 
						nama2='$_POST[nama2]', 
						jabatan2='$_POST[jabatan2]', 
						alamat2='$_POST[alamat2]', 
						tlp2='$_POST[tlp2]', 
						nama3='$_POST[nama3]', 
						jabatan3='$_POST[jabatan3]', 
						alamat3='$_POST[alamat3]', ";
				if($aktares!="") $query .="copyakta='$aktares', ";
				if($siupres!="") $query .="copysiup='$siupres', ";
				if($tdpres!="") $query .="copytdp='$tdpres', ";
				if($domisilires!="") $query .="copydomisili='$domisilires', ";
				if($npwpres!="") $query .="copynpwp='$npwpres', ";
				if($ktp1res!="") $query .="copyktp1='$ktp1res', ";
				if($ktp2res!="") $query .="copyktp2='$ktp2res', ";
				if($simres!="") $query .="copysim='$simres', ";
				if($pores!="") $query .="copypo='$pores', ";
				if($tabunganres!="") $query .="copytabungan='$tabunganres', ";
				if($kkres!="") $query .="copykk='$kkres', ";
				if($pbbres!="") $query .="copypbb='$pbbres', ";
				if($suratres!="") $query .="copysurat='$suratres' , ";
				$query .="tlp3='$_POST[tlp3]'
					WHERE id='$_POST[idx]'";
			break;
		default :
			$query = "delete from gl_kontak where id='$_POST[id]'";
			break;
	}
	mysql_select_db($database_con_gl, $con_gl);
	$runquery = mysql_query($query, $con_gl);
	if($runquery) {
		$alert = "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil "; if($act=='add' OR $act=='edit') { $alert .="disimpan"; }else{ $alert .= "dihapus"; } $alert .= " ...";
	}else{
		$alert = "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal disimpan !!!";
	}
}
?>
<?php if($runquery) { ?>
<script language="javascript">
window.location.href='../components/customercorporate/input.php';
window.parent.document.getElementById('result').style.display='block'; 
window.parent.document.getElementById('result').innerHTML='<?php echo $alert; ?>';
window.parent.setTimeout("location.href='<?php echo $url;?>'", 2000);
</script>
<!-- <script type="text/javascript">setTimeout("location.href='<?php echo $url;?>'", 2000);</script> -->
<?php } ?>